package com.zret.oasys.operation.leave;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zret.oasys.dbtools.MD5Util;
import com.zret.oasys.dbtools.UniqueIDtools;
import com.zret.oasys.entity.Emp;
import com.zret.oasys.entity.Leavedetails;
import com.zret.oasys.entity.Leavetable;
import com.zret.oasys.json.entity.FaceLoginJson;
import com.zret.oasys.json.entity.MsgJson;
import com.zret.oasys.service.EmpService;
import com.zret.oasys.service.impl.EmpServiceImpl;

public class CreatLeave implements LeaveServletOperation {
	
	private static SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Override
	public void doOperation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		
		//判断表单是否重复提交
		String tokenSession = request.getSession().getAttribute("token") + "";
		String tokenRequest = request.getParameter("token");
		
		System.out.println(tokenSession);
		System.out.println(tokenRequest);
		
		PrintWriter out = response.getWriter();
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		MsgJson msg = new MsgJson();
		
		if(tokenRequest == null || tokenSession == null || !tokenRequest.equals(tokenSession)) {
			msg.setMsg("repeat");
			msg.setCode(222);
		}else if(request.getParameter("empid") == null) {
			msg.setMsg("faile");
			msg.setCode(500);
		} else if(request.getParameter("empname") == null ) { 
			msg.setMsg("faile");
			msg.setCode(500);
		} else if(request.getParameter("leavetype") == null) {
			msg.setMsg("faile");
			msg.setCode(500);
		} else if(request.getParameter("leavetype") == null) {
			msg.setMsg("faile");
			msg.setCode(500);
		} else if(request.getParameter("starttime") == null) {
			msg.setMsg("faile");
			msg.setCode(500);
		} else if(request.getParameter("endtime") == null) {
			msg.setMsg("faile");
			msg.setCode(500);
		} else if(request.getParameter("apprid") == null) {
			msg.setMsg("faile");
			msg.setCode(500);
		} else if(request.getParameter("leavecomment") == null) {
			msg.setMsg("faile");
			msg.setCode(500);
		} else {
			int empid = Integer.valueOf(request.getParameter("empid"));
			//String empname = request.getParameter("empname");
			String leavetype = request.getParameter("leavetype");
			String starttime = request.getParameter("starttime");
			String endtime = request.getParameter("endtime");
			int apprid = Integer.valueOf(request.getParameter("apprid"));
			String leavecomment = request.getParameter("leavecomment");
			// 看看日期对不对
			System.out.println(starttime);
			System.out.println(endtime);
			// 生成流水号
			String leavenum = UniqueIDtools.getUUID();
			// 创建请假单对象
			Leavetable leavetable = new Leavetable(leavenum, empid, starttime, leavecomment, endtime, leavetype, "待审批", "");
			// 创建审批单对象
			Leavedetails leavedetails = new Leavedetails(leavenum, 1, apprid, "", simple.format(new Date()), "待审批");
			// 数据库事务操作, 增加数据
			boolean flag = leaveService.creatLeave(leavetable, leavedetails);
			// 返回信息
			if(flag) {
				msg.setMsg("ture");
				msg.setCode(200);
			} else {
				msg.setMsg("false");
				msg.setCode(200);
			}
			String msgJson = gson.toJson(msg);
			out.print(msgJson);
			out.close();
		}
	}

}
